using System;
using System.Collections.Generic;
using System.Text;

namespace HtmlAgilityPack
{
    /// <summary>
    /// 
    /// </summary>
    public class HtmlDocSearch
    {
        private HtmlDocument m_doc = null;

        /// <summary>
        /// Initializes a new instance of the <see cref="HtmlDocSearch"/> class.
        /// </summary>
        /// <param name="doc">The doc.</param>
        public HtmlDocSearch(HtmlDocument doc)
        {
            m_doc = doc;
        }

        /// <summary>
        /// Searches for tags.
        /// </summary>
        /// <param name="TagName">Name of the tag.</param>
        /// <returns></returns>
        public HtmlNodeCollection SearchForTags(string TagName)
        {
            HtmlNodeCollection nodeList = new HtmlNodeCollection(m_doc.DocumentNode);
            string sTagName = TagName.ToLower();
            SearchForTagsInternal(m_doc.DocumentNode, sTagName, nodeList);
            return nodeList;            
        }

        /// <summary>
        /// Searches for tags internal.
        /// </summary>
        /// <param name="RootNode">The root node.</param>
        /// <param name="TagName">Name of the tag.</param>
        /// <param name="nodeList">The node list.</param>
        private void SearchForTagsInternal(HtmlNode RootNode, string TagName, HtmlNodeCollection nodeList)
        {            
            foreach (HtmlNode node in RootNode.ChildNodes)
            {
                if (node.Name.ToLower() == TagName)
                    nodeList.Add(node);

                if (node.HasChildNodes)
                    SearchForTagsInternal(node, TagName, nodeList);
            }
        }

        /// <summary>
        /// Searches for attributes.
        /// </summary>
        /// <param name="AttributeName">Name of the attribute.</param>
        /// <returns></returns>
        public HtmlAttributeCollection SearchForAttributes(string AttributeName)
        {
            HtmlAttributeCollection attributeList = new HtmlAttributeCollection(m_doc.DocumentNode);
            string sAttributeName = AttributeName.ToLower();
            SearchForAttributesInternal(m_doc.DocumentNode, sAttributeName, attributeList);
            return attributeList;
        }

        /// <summary>
        /// Searches for attributes internal.
        /// </summary>
        /// <param name="RootNode">The root node.</param>
        /// <param name="AttributeName">Name of the attribute.</param>
        /// <param name="attributeList">The attribute list.</param>
        private void SearchForAttributesInternal(HtmlNode RootNode, string AttributeName, HtmlAttributeCollection attributeList)
        {
            foreach (HtmlNode node in RootNode.ChildNodes)
            {
                if (node.HasAttributes)
                {
                    foreach (HtmlAttribute att in node.Attributes)
                    {
                        if (att.Name.ToLower() == AttributeName)
                        {
                            attributeList.AppendEx(att);
                        }
                    }
                }
                
                if (node.HasChildNodes)
                    SearchForAttributesInternal(node, AttributeName, attributeList);
            }
        }
    }
}
